<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 服务员销售明细列表
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    /* 检查权限 */
    admin_priv('users_manage');

    $smarty->assign('ur_here',      '销售明细');

    $sales_list = sales_list();

    $smarty->assign('sales_list',    $sales_list['sales_list']);
    $smarty->assign('filter',       $sales_list['filter']);
    $smarty->assign('record_count', $sales_list['record_count']);
    $smarty->assign('page_count',   $sales_list['page_count']);
    $smarty->assign('full_page',    1);
    $smarty->assign('sort_id', '<img src="images/sort_desc.gif">');
    $smarty->assign('sort_money_paid', '<img src="images/sort_desc.gif">');

    assign_query_info();
    $smarty->display('sales_list.htm');
}

/*后台佣金记录*/
elseif ($_REQUEST['act'] == 'commission_log') {
    $shop_id = $_REQUEST['shop_id'];
    $commission_log = commission_log();
    $smarty->assign('ur_here',      '佣金记录');
    $smarty->assign('commission_list',$commission_log['commission_list']);
    $smarty->assign('filter',$commission_log['filter']);
    $smarty->assign('record_count',$commission_log['record_count']);
    $smarty->assign('page_count',$commission_log['page_count']);
    $smarty->assign('full_page',1);

    assign_query_info();
    $smarty->display('commission_log.htm');
}
/*后台商家粉丝列表*/
elseif ($_REQUEST['act'] == 'fans_list') {
    $shop_id = $_REQUEST['shop_id'];
    $fans_list = fans_list();
    $smarty->assign('usr_here','粉丝列表');
    $smarty->assign('fans_list',$fans_list['fans_list']);
    $smarty->assign('filter',$fans_list['filter']);
    $smarty->assign('record_count',$fans_list['record_count']);
    $smarty->assign('page_count',$fans_list['page_count']);
    $smarty->assign('full_page',1);
    $smarty->assign('shop_id',$shop_id);
    assign_query_info();
    $smarty->display('fans_list.htm');
}
/*成为waiter*/
elseif ($_REQUEST['act'] == 'be_waiter') {
    $user_id  = $_REQUEST['user_id'];
    $shop_id = $_REQUEST['shop_id'];
    $GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET is_waiter = 1 AND from_shop = "'.$shop_id.'" WHERE user_id = "'.$user_id.'"');
    sys_msg('操作完成');
}


/*------------------------------------------------------ */
//-- ajax返回销售明细
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $sales_list = sales_list();

    $smarty->assign('sales_list',    $sales_list['sales_list']);
    $smarty->assign('filter',       $sales_list['filter']);
    $smarty->assign('record_count', $sales_list['record_count']);
    $smarty->assign('page_count',   $sales_list['page_count']);

    $sort_flag  = sort_flag($sales_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('sales_list.htm'), '', array('filter' => $sales_list['filter'], 'page_count' => $sales_list['page_count']));
}


/**
 *  返回用户列表数据
 *
 * @access  public
 * @param
 *
 * @return void
 */
function sales_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['shop_name'] = empty($_POST['shop_name']) ? '' : trim($_POST['shop_name']);
        $filter['waiter_name'] = empty($_POST['waiter_name']) ? '' : trim($_POST['waiter_name']);
        $filter['rank'] = empty($_REQUEST['rank']) ? 0 : intval($_REQUEST['rank']);
        $filter['pay_points_gt'] = empty($_REQUEST['pay_points_gt']) ? 0 : intval($_REQUEST['pay_points_gt']);
        $filter['pay_points_lt'] = empty($_REQUEST['pay_points_lt']) ? 0 : intval($_REQUEST['pay_points_lt']);
        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        $ex_where = ' WHERE 1 ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND user_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }
        if ($filter['rank'])
        {
            $sql = "SELECT min_points, max_points, special_rank FROM ".$GLOBALS['ecs']->table('user_rank')." WHERE rank_id = '$filter[rank]'";
            $row = $GLOBALS['db']->getRow($sql);
            if ($row['special_rank'] > 0)
            {
                /* 特殊等级 */
                $ex_where .= " AND user_rank = '$filter[rank]' ";
            }
            else
            {
                $ex_where .= " AND rank_points >= " . intval($row['min_points']) . " AND rank_points < " . intval($row['max_points']);
            }
        }
        
        /*加入商家编号搜索*/
        if ($_REQUEST['shop_id']) {
            $_SESSION['find_shop_id'] = $_REQUEST['shop_id'];
        }
        if (!empty($_SESSION['find_shop_id'])) {
            $ex_where .=" AND shop_id = '$_SESSION[find_shop_id]' ";
        }

        /*加入店员过滤*/
        if ($_REQUEST['waiter_id']) {
            $_SESSION['find_waiter_id'] = $_REQUEST['waiter_id'];
        }else{
            $_SESSION['find_waiter_id'] = '';
        }

        /*搜索添加店铺名*/
        if ($filter['shop_name'] != '') {
            $shop_id = $GLOBALS['db']->getOne('SELECT goods_id FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_name LIKE "%'.$filter['shop_name'].'%" ');
            if ($shop_id) {
                $_SESSION['find_shop_id'] = $shop_id;
            }else
            {
                $_SESSION['find_shop_id'] = -1;
            }
        }else{
            $_SESSION['find_shop_id'] = '';
        }

        /*搜索添加店员名*/
        if ($filter['waiter_name'] != '') {
            $waiter_id = $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('users').' WHERE user_name LIKE "%'.$filter['waiter_name'].'%"');
            if ($waiter_id) {
                $_SESSION['find_waiter_id'] = $waiter_id;
            }else
            {
                $_SESSION['find_waiter_id'] = -1;
            }
        }else{
            $_SESSION['find_waiter_id'] = '';   
        }

        if (!empty($_SESSION['find_waiter_id'])) {
            $ex_where .= " AND waiter_id = '$_SESSION[find_waiter_id]'";
        }
        if (!empty($_SESSION['find_shop_id'])) {
            $ex_where .= " AND shop_id = '$_SESSION[find_shop_id]' ";
        }

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('sales_log') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT * ".
                " FROM " . $GLOBALS['ecs']->table('sales_log') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];
        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $sales_list = $GLOBALS['db']->getAll($sql);

    $count = count($sales_list);
    for ($i=0; $i<$count; $i++)
    {
        $sales_list[$i]['add_time_format'] = local_date($GLOBALS['_CFG']['date_format'], $sales_list[$i]['add_time']);
        $sales_list[$i]['user_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = '.$sales_list[$i]['user_id'].'');
        $sales_list[$i]['shop_name'] = $GLOBALS['db']->getOne('SELECT goods_name FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = '.$sales_list[$i]['shop_id'].'');
        $sales_list[$i]['waiter_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = '.$sales_list[$i]['waiter_id'].'');
    }

    $arr = array('sales_list' => $sales_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}


/**
 * 佣金记录列表
 * @return array 佣金记录数组
 */
function commission_log()
{   
    $result = get_filter();
    if ($result === false) 
    {
        $ex_where = " WHERE 1 ";
        $filter['shop_id'] = empty($_REQUEST['shop_id']) ? '' : trim($_REQUEST['shop_id']);
        $filter['user_id'] = empty($_REQUEST['user_id']) ? '' : trim($_REQUEST['user_id']);

        if ($filter['shop_id']) {
            //查找到店主
            $user_id = $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$filter['shop_id'].'"');
            $ex_where .=" AND user_id = '$user_id' ";
        }
        if ($filter['user_id']) {
            $ex_where .= " AND user_id = '$filter[user_id]' ";
        }
        //记录数
        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('commission_log') . $ex_where);
        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT * ".
                " FROM " . $GLOBALS['ecs']->table('commission_log') . $ex_where .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];
        //filter
        set_filter($filter, $sql);

    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $commission_log = $GLOBALS['db']->getAll($sql);

    //数据处理
    foreach ($commission_log as $k => $v) {
        $commission_log[$k]['user_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
        $commission_log[$k]['add_time_format'] = date('Y-m-d',$v['add_time']);
        switch ($v['yj_type']) {
            case '1':
                $commission_log[$k]['type_name'] = '用户佣金';
                break;
             case '2':
                $commission_log[$k]['type_name'] = '服务员佣金';
                break;   
            case '3':
                $commission_log[$k]['type_name'] = '商家佣金';
                break;
            case '4':
                $commission_log[$k]['type_name'] = '区域代理佣金';
                break;
            default:
                break;
        }

    }
    $arr = array('commission_list' => $commission_log, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}


/**
 * 粉丝列表
 * @return array 粉丝列表
 */
function fans_list()
{
    $result = get_filter();
    if ($result === false) 
    {
        $ex_where = " WHERE 1 ";
        $filter['shop_id'] = empty($_REQUEST['shop_id']) ? '' : trim($_REQUEST['shop_id']);

        if ($filter['shop_id']) {
            $ex_where .=" AND shop_id = '$filter[shop_id]'";
        }
        //记录数
        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('shop_fans') . $ex_where);
        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT * ".
                " FROM " . $GLOBALS['ecs']->table('shop_fans') . $ex_where .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];
        //filter
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $fans_list = $GLOBALS['db']->getAll($sql);

    //数据处理
    foreach ($fans_list as $k => $v) {
       $user_info = $GLOBALS['db']->getRow('SELECT user_name, is_waiter, reg_time FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
       $fans_list[$k]['user_name'] = $user_info['user_name'];
       $fans_list[$k]['is_waiter'] = $user_info['is_waiter'];
       $fans_list[$k]['reg_time'] = date('Y-m-d',$user_info['reg_time']);
       $fans_list[$k]['sum_pay'] = $GLOBALS['db']->getOne('SELECT SUM(money_paid) FROM '.$GLOBALS['ecs']->table('sales_log').' WHERE shop_id = "'.$filter['shop_id'].'" AND user_id = "'.$v['user_id'].'"');
    }

    $arr = array('fans_list' => $fans_list , 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}
/**
 * 获取指定类型地区列表
 * @param  integer $type 地区类型
 * @return array        地区列表
 */
function get_area_list($type = 0)
{
    return $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('region').' WHERE region_type = '.$type);
}
?>